﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using MultiStepDemo.Utils;
using MultiStepDemo.ViewModels;

namespace MultiStepDemo.Controllers
{
    public class TestingController : Controller
    {
        //
        // GET: /Testing/

        public ActionResult Index()
        {
            var questions = GetQuestion(1, 5);

            var viewModel = new TestingViewModel() { Questions = questions, PageIndex = 1, StartTime = DateTime.Now, TestTime = 1 };

            viewModel.TimeLeft = (long)viewModel.StartTime.AddMinutes(viewModel.TestTime).Subtract(DateTime.Now).TotalSeconds;

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Index(TestingViewModel viewModelparam)
        {
            //validate the time is empty then save result to db
            if (DateTime.Now.Subtract(viewModelparam.StartTime).TotalMinutes >= viewModelparam.TestTime)
                return Index();

            if (!string.IsNullOrEmpty(viewModelparam.SelectedAnswer) && !string.IsNullOrEmpty(viewModelparam.SelectedQuestion))
            {
                if (Session[Constants.RESULT_LIST] == null)
                    Session[Constants.RESULT_LIST] = new Dictionary<int, int>();

                var tmpResult = (Dictionary<int, int>)Session[Constants.RESULT_LIST];

                //get question id
                string[] arrQuestions = viewModelparam.SelectedQuestion.Split('|');

                //get answer id
                string[] arrAnswers = viewModelparam.SelectedAnswer.Split('|');

                for (var i = 0; i < arrQuestions.Length - 1; i++)
                {
                    var questionId = Convert.ToInt32(arrQuestions[i]);
                    var answerId = Convert.ToInt32(arrAnswers[i]);
                    //validate that the key of question has not existed then add into result list
                    if (!tmpResult.ContainsKey(questionId))
                        tmpResult.Add(questionId, answerId);
                    //else if existed then update answer id
                    else
                    {
                        tmpResult[questionId] = answerId;
                    }
                }

                //set resutl to session
                Session[Constants.RESULT_LIST] = tmpResult;
            }

            var questions = GetQuestion(viewModelparam.PageIndex, 5);

            var viewModel = new TestingViewModel() { Questions = questions, PageIndex = viewModelparam.PageIndex, TestTime = 1 };

            viewModel.TimeLeft = (long)viewModelparam.StartTime.AddMinutes(viewModel.TestTime).Subtract(DateTime.Now).TotalSeconds;

            return View(viewModel);
        }

        public ActionResult Paging(int id)
        {
            var questions = GetQuestion(id, 5);

            var viewModel = new TestingViewModel() { Questions = questions };

            return View("Index", viewModel);
        }

        private List<QuestionViewModel> GetQuestion(int pageIndex, int pageSize)
        {
            int totalRecord = 20;

            ViewData[Constants.PAGE_INDEX] = pageIndex;

            ViewData[Constants.TOTAL_PAGE] = totalRecord / pageSize;

            var answers = new List<AnswerViewModel>();

            for (var i = 1; i <= 4; i++)
                answers.Add(new AnswerViewModel() { Content = "Answer content " + i.ToString(), Id = i });

            var questions = new List<QuestionViewModel>();

            var fromResult = (pageIndex - 1) * pageSize;

            if (Session[Constants.RESULT_LIST] == null)
                Session[Constants.RESULT_LIST] = new Dictionary<int, int>();

            var tmpResult = (Dictionary<int, int>)Session[Constants.RESULT_LIST];
            for (var i = fromResult; i < fromResult + pageSize && i < totalRecord; i++)
            {
                //set the key
                var questionId = i + 1;
                questions.Add(new QuestionViewModel() { Title = "Title " + questionId.ToString(), Answers = answers, Id = questionId });

                //get result from memory if existed
                if (tmpResult.ContainsKey(questionId))
                    questions[i - fromResult].SelectedAnswer = tmpResult[questionId];
                else
                    questions[i - fromResult].SelectedAnswer = 0;
            }

            return questions;
        }
    }
}